﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FIleBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // 初始化根目录，这里以C盘为例
            string rootDirectory = "C:\\";

            // 填充树形视图
            FillTreeView(rootDirectory);

            // 绑定树形视图的选择事件
            treeView.SelectedItemChanged += TreeView_SelectedItemChanged;
        }

        private void FillTreeView(string directory)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(directory);
            var rootNode = new TreeViewItem
            {
                Header = dirInfo.Name,
                Tag = dirInfo.FullName
            };
            treeView.Items.Add(rootNode);

            // 递归填充子文件夹
            foreach (var subDirInfo in dirInfo.GetDirectories())
            {
                var subNode = new TreeViewItem
                {
                    Header = subDirInfo.Name,
                    Tag = subDirInfo.FullName
                };
                rootNode.Items.Add(subNode);
            }
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var selectedNode = (TreeViewItem)e.NewValue;
            string selectedPath = (string)selectedNode.Tag;

            if (Directory.Exists(selectedPath))
            {
                // 如果选择的是文件夹，填充文件列表
                FillFileList(selectedPath);
            }
            else if (File.Exists(selectedPath))
            {
                // 如果选择的是文件
                if (selectedPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                {
                    // 如果是 .exe 文件，执行它
                    ExecuteFile(selectedPath);
                }
                else if (selectedPath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // 如果是 .txt 文件，使用默认关联程序打开
                    OpenTextFile(selectedPath);
                }
            }
        }

        private void FillFileList(string directory)
        {
            listView.Items.Clear();
            DirectoryInfo dirInfo = new DirectoryInfo(directory);

            foreach (var fileInfo in dirInfo.GetFiles())
            {
                listView.Items.Add(new
                {
                    Name = fileInfo.Name,
                    Type = fileInfo.Extension
                });
            }
        }
        private void ExecuteFile(string filePath)
        {
            try
            {
                System.Diagnostics.Process.Start(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"无法执行文件：{ex.Message}");
            }
        }

        private void OpenTextFile(string filePath)
        {
            try
            {
                System.Diagnostics.Process.Start("notepad.exe", filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"无法打开文本文件：{ex.Message}");
            }
        }
    }
    }
